<?php
namespace app\api\controller;

use think\Db;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class Doubai
{

    private static function surfNet($query) {
       $headers = [
                'headers' => [
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                    'Accept-Encoding' => 'gzip, deflate, br',
                    'Accept-Language' => 'zh-CN,zh;q=0.9',
                    'Cache-Control' => 'no-cache',
                    'Host' => 'movie.douban.com',
                    'User-Agent' => 'Mozilla/5.0 (Linux; U; Android 4.4.3; zh-cn; HTC 802w Build/KTU84L) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025489 Mobile Safari/533.1 MicroMessenger/6.3.13.49_r4080b63.740 NetType/WIFI Language/zh_CN',
                    'Connection' => 'keep-alive',
                    'Upgrade-insecure-requests' => 1,
                    'Pragma' => 'no-cache'
                ],
                'verify'=>false
       ];

        $client = new Client();

        $response= $client->request('GET',$query['url'],$headers);

        if($response->getStatusCode() == 200){
            return $response->getBody()->getContents();
        }else{
            return false;
        }
    }


    public function index(){

        $param=[];
        $param['url']='https://movie.douban.com/cinema/nowplaying/hangzhou/';
        $content = self::surfNet($param);
        $crawler = new Crawler($content);

        $data=[];
        $crawler->filter('#nowplaying .lists .list-item')->each(function (Crawler $node,$i) use (&$data){
                $movieName = trim($node->attr('data-title'));
                $movieNum = trim($node->attr('data-score'));
                $data[]=['name'=>$movieName,'num'=>$movieNum];
        });

        $list= Db::name('movie')->whereBetween('createtime',[date('Y-m-d'),date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d'),date('Y')))])->column('name','id');


        foreach ($list as $k=>$v){
            Db::name('movie')->where('id',$k)->update(['grade'=>0]);
            foreach ($data as $val){
                similar_text($val['name'],$v,$percent);
                if($percent>90){
                    Db::name('movie')->where('id',$k)->update(['grade'=>$val['num']]);
                    break;
                }
            }
        }

        $list= Db::name('movie')->whereBetween('createtime',[date('Y-m-d'),date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d'),date('Y')))])->select();

        $info = '';
        foreach ($list as $k=>$v){
            if($k==0){
                $info.='今日热映讯息：'."\n".$v['name']."\t".'累计票房：'.$v['all_ticket']."\t".$v['pulish_date']."\t".'豆瓣评分：'.$v['grade']."\n";
            }else{
                $info.= "\t\t".$v['name']."\t".'累计票房：'.$v['all_ticket']."\t".$v['pulish_date']."\t".'豆瓣评分：'.$v['grade'];
            }
        }

        Db::name('movie_info')->insert(['info'=>$info,'createtime'=>date('Y-m-d H:i:s')]);

        echo '完成';

    }


}
